﻿#include "weatherwdiget.h"
#include "ui_weatherwdiget.h"

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#include <QMessageBox>
#include <QNetworkReply>

WeatherWdiget::WeatherWdiget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WeatherWdiget)
{
    ui->setupUi(this);
    mNetworkAccessManager = new QNetworkAccessManager(this);
    connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &WeatherWdiget::onReplied);

    ui->labelTips->setWordWrap(true);


    //根据keys,设置icon的路径
    //根据keys,设置icon的路径
    mTypeMap.insert("暴雪",":/res/type/BaoXue.png");
    mTypeMap.insert("暴雨",":/res/type/BaoYu. png");
    mTypeMap.insert("暴雨到大暴雨",":/res/type/BaoYuDaoDaBaoYu.png");
    mTypeMap.insert("大暴雨",":/res/type/DaBaoYu.png");
    mTypeMap.insert("大暴雨到特大暴雨",":/res/type/DaBaoYuDaoTeDaBaoYu.png");
    mTypeMap.insert("大到暴雪",":/res/type/DaDaoBaoXue.png");
    mTypeMap.insert("大雪",":/res/type/DaXue.png");
    mTypeMap.insert("大雨",":/res/type/DaYu.png");
    mTypeMap.insert("冻雨",":/res/type/DongYu.png");
    mTypeMap.insert("多云",":/res/type/DuoYun.png");
    mTypeMap.insert("浮沉",":/res/type/FuChen.png");
    mTypeMap.insert("雷阵雨",":/res/type/LeiZhenYu.png");
    mTypeMap.insert("雷阵雨伴有冰雹",":/res/type/LeiZhenYuBanYouBingBao.png");
    mTypeMap.insert("霾",":/res/type/Mai.png");
    mTypeMap.insert("强沙尘暴",":/res/type/QiangShaChenBao.png");
    mTypeMap.insert("晴",":/res/type/Qing.png");
    mTypeMap.insert("沙尘暴",":/res/type/ShaChenBao.png");
    mTypeMap.insert("特大暴雨",":/res/type/TeDaBaoYu.png");
    mTypeMap.insert("undefined",":/res/type/undefined.png");
    mTypeMap.insert("雾",":/res/type/Wu.png");
    mTypeMap.insert("小到中雪",":/res/type/XiaoDaoZhongXue.png");
    mTypeMap.insert("小到中雨",":/res/type/XiaoDaoZhongYu.png");
    mTypeMap.insert("小雪",":/res/type/XiaoXue.png");
    mTypeMap.insert("小雨",":/res/type/XiaoYu.png");
    mTypeMap.insert("雪",":/res/type/Xue.png");
    mTypeMap.insert("扬沙",":/res/type/YangSha.png");
    mTypeMap.insert("阴",":/res/type/Yin.png");
    mTypeMap.insert("雨",":/res/type/Yu.png");
    mTypeMap.insert("雨夹雪",":/res/type/YuJiaXue.png");
    mTypeMap.insert("阵雪",":/res/type/ZhenXue.png");
    mTypeMap.insert("阵雨",":/res/type/ZhenYu.png");
    mTypeMap.insert("中到大雪",":/res/type/ZhongDaoDaXue.png");
    mTypeMap.insert("中到大雨",":/res/type/ZhongDaoDaYu.png");
    mTypeMap.insert("中雪",":/res/type/ZhongXue.png");
    mTypeMap.insert("中雨",":/res/type/ZhongYu.png");

    //直接在构造函数中,请求天气数据
    getWeatherInfo("北京");//北京
}

WeatherWdiget::~WeatherWdiget()
{
    delete ui;
}

//获取天气信息
void WeatherWdiget::getWeatherInfo(QString cityName)
{

    QUrl url = QUrl("http://t.weather.itboy.net/api/weather/city/101280601");
    mNetworkAccessManager->get(QNetworkRequest(url));

}

void WeatherWdiget::onReplied(QNetworkReply *reply)
{
    //状态码
    int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    //判断是否请求成功
    if (reply->error() != QNetworkReply::NoError || statusCode != 200) {
        QMessageBox::warning(this, "天气", reply->errorString(), QMessageBox::Ok);
    }else{
        QByteArray requestData = reply->readAll();
        //qDebug() << requestData.data();
        //QJsonDocument doc = QJsonDocument::fromJson(requestData);
        parseJson(requestData);//解析json数据,创建2个类,ToDay和Day(数组)
    }
    //释放内存
    reply->deleteLater();
}
int WeatherWdiget::parseString(QString str)
{
    str = str.split(" ").at(1);//以空格为分割符,去除前面的文字,取出第二个X℃,
    str = str.left(str.length() - 1);//去除后面的摄氏度符号
    return str.toInt();
}

void WeatherWdiget::parseJson(QByteArray data)
{
    //解析错误时会触发的
    QJsonParseError err;
    //创建QJsonDocument
    QJsonDocument doc = QJsonDocument::fromJson(data, &err);
    if (err.error != QJsonParseError::NoError) {
        qDebug("%s(%d): %s", __FUNCTION__, __LINE__, err.errorString().toLatin1().data());
        return;
    }
    //Json对象,包括了请求的全部数据
    QJsonObject rootObj = doc.object();
    //qDebug() << rootObj;
    QJsonObject dataObj = rootObj.value("data").toObject();
    QJsonObject yesterdayObj = dataObj.value("yesterday").toObject();
    //Json数组,未来几天的数据
    QJsonArray forecastArray = dataObj.value("forecast").toArray();
    //1.解析日期和城市
    ui->labelCity->setText( rootObj.value("cityInfo").toObject().value("city").toString() );
    //qDebug() << mToday.date << mToday.city;

    mDay[0].low = parseString(yesterdayObj.value("low").toString());//最低温度
    mDay[0].high = parseString(yesterdayObj.value("high").toString());//最高温度
    mDay[0].fx = yesterdayObj.value("fx").toString();//风向
    mDay[0].fl = yesterdayObj.value("fl").toString();//风力
    //qDebug() << mDay[0].date << mDay[0].aqi << mDay[0].type << mDay[0].week << mDay[0].low << mDay[0].high << mDay[0].fx << mDay[0].fl;
    //3.解析forecast中的6天数据
    //qDebug() << forecastArray.size();
    for (int i = 0; i < 5; ++i) {
        QJsonObject forecastData = forecastArray[i].toObject();
        //跳过0,因为0已经赋值给昨天的数据了
        mDay[i + 1].date = forecastData.value("date").toString();//日期
        mDay[i + 1].aqi = forecastData.value("aqi").toDouble();//空气质量,toDouble包括整数型
        mDay[i + 1].type = forecastData.value("type").toString();//天气类型
        mDay[i + 1].week = forecastData.value("week").toString();//星期
        mDay[i + 1].low = parseString(forecastData.value("low").toString());//最低温度
        mDay[i + 1].high = parseString(forecastData.value("high").toString());//最高温度
        mDay[i + 1].fx = forecastData.value("fx").toString();//风向
        mDay[i + 1].fl = forecastData.value("fl").toString();//风力
        mDay[i + 1].ymd = forecastData.value("ymd").toString();//完整年月日
        //ymd分割成MM/dd
        //QDateTime::fromString(forecastData.value("ymd").toString(), "yyyy-MM-dd").toString("MM/dd");//日期
        //qDebug() << i << mDay[i + 1].date << mDay[i + 1].aqi << mDay[i + 1].type << mDay[i + 1].week << mDay[i + 1].low << mDay[i + 1].high << mDay[i + 1].fx << mDay[i + 1].fl;
        //qDebug() << i << mDay[i].date;
        //qDebug() << mDay[i + 1].ymd;
    }
    //4.解析今天的数据
    mToday.shidu = dataObj.value("shidu").toString();
    mToday.quality = dataObj.value("quality").toString();
    mToday.ganmao = dataObj.value("ganmao").toString();
    mToday.pm25 = dataObj.value("pm25").toDouble();
    mToday.wendu = dataObj.value("wendu").toString().toDouble();
    //qDebug() << "mToday.wendu" << QString::number(mToday.wendu);
    //qDebug() << mToday.shidu << mToday.quality << mToday.ganmao << mToday.pm25 << mToday.wendu;
    //注意:forecast中的第2个元素也是今天的数据
    mToday.type = mDay[1].type;
    mToday.low = mDay[1].low;
    mToday.high = mDay[1].high;
    mToday.fx = mDay[1].fx;
    mToday.fl = mDay[1].fl;
    //qDebug() << mToday.type << mToday.low << mToday.high << mToday.fx << mToday.fl;

    ui->labelTips->setText(mToday.ganmao);
    ui->labelTodyaLow->setText("/"+QString::number(mToday.low)+"℃");
    ui->labelTodayTemp->setText(QString::number(mToday.wendu));
    ui->labelweaIcon->setPixmap(mTypeMap[mToday.type]);
    QDate date = QDate::fromString(mDay[1].ymd, "yyyy-MM-dd");

    ui->labelday0Low->setText("/"+QString::number(mToday.low)+"℃");
    ui->labelday0High->setText(QString::number(mToday.high));
    ui->labelday0Icon->setPixmap(mTypeMap[mToday.type]);
    ui->labelday0Date->setText(date.toString("MM/dd"));
    ui->labelday0wea->setText(mToday.type);

    ui->labelday1wea->setText(mDay[2].type);
    ui->labelday1Low->setText("/"+QString::number(mDay[2].low)+"℃");
    ui->labelday1High->setText(QString::number(mDay[2].high));
    ui->labelday1Icon->setPixmap(mTypeMap[mDay[2].type]);
    date = QDate::fromString(mDay[2].ymd, "yyyy-MM-dd");
    ui->labelday1Date->setText(date.toString("MM/dd"));

    ui->labelday3wea->setText(mDay[3].type);
    ui->labelday3Low->setText("/"+QString::number(mDay[3].low)+"℃");
    ui->labelday3High->setText(QString::number(mDay[3].high));
    ui->labelday3Icon->setPixmap(mTypeMap[mDay[3].type]);
    date = QDate::fromString(mDay[3].ymd, "yyyy-MM-dd");
    ui->labelday3Date->setText(date.toString("MM/dd"));

}
